<?php
error_reporting(7);

if ($_GET[action] == "lastupdate" OR $_GET[action] == "pop" OR $_GET[action] == "hot") {
        $templatelist = "searchresult,navbar_searchresult,articlelistbit,articlelistbit_img,pagenav,searchresult_articlelist ";
        require "global.php";
        cachesorts();

        $sortlist = makesortlist();

        eval("\$navbit = \"" . gettemplate('navbar_joiner') . "\";");
        eval("\$navbit .= \"" . gettemplate('navbar_searchresult') . "\";");
        eval("\$navbar = \"" . gettemplate('navbar') . "\";");

        unset($articlelist);

        $perpage = $searchperpage;

        $total = $DB->fetch_one_array("SELECT COUNT(*) AS count FROM " . $db_prefix . "article");

        $totalresults = $total[count];

        $totalpages = ceil($total[count] / $perpage);

        if ($pagenum < 1 OR empty($pagenum)) {
                $pagenum = 1;
        } elseif ($pagenum > $totalpages) {
                $pagenum = $totalpages;
        } 

        $offset = ($pagenum-1) * $perpage;

        if ($totalresults > 0) {
                $from = $offset + 1;
                if ($pagenum == $totalpages) {
                        $to = $totalresults;
                } else {
                        $to = $offset + $perpage;
                } 
        } else {
                $from = 0;
                $to = 0;
        } 

        if ($_GET[action] == "hot") {
                $order = "views";
        } elseif ($_GET[action] == "pop") {
                $order = "totalscore";
        } else {
                $order = "date";
        } 
        $articles = $DB->query("SELECT * FROM " . $db_prefix . "article
                                   WHERE visible=1
                                   ORDER BY $order DESC
                                   LIMIT $offset,$perpage");

        if ($DB->num_rows($articles) > 0) {
                $counter = 0;
                $row = 0; 
                // $division = $sortinfo[division_article];
                $division = 1;
                $tablewidth = floor(100 / $division);

                while ($article = $DB->fetch_array($articles)) {
                        $article[time] = padate($timeformat_article, $article[date]);
                        $article[date] = padate($dateformat_article, $article[date]);
                        if (!empty($article[imageid])) {
                                // $article[img] = "<img src=\"showimg.php?iid=$article[imageid]\" border=\"0\" vspace=\"2\" hspace=\"2\" align=\"left\">";
                                eval("\$article[img] = \"" . gettemplate('articlelistbit_img') . "\";");
                        } else {
                                $article[img] = "";
                        } 
                        if ($counter == 0) {
                                if ($row++ % 2 == 0) {
                                        $bgcolor = "$style[firstalt]";
                                } else {
                                        $bgcolor = "$style[secondalt]";
                                } 
                                $articlelistbit .= "<tr bgcolor=\"$bgcolor\" align=\"center\">";
                        } 
                        $articlelistbit .= "<td nowrap valign=\"top\" width=\"$tablewidth%\">";
                        eval("\$articlelistbit .= \"" . gettemplate('articlelistbit') . "\";");

                        $articlelistbit .= "</td>\n";

                        if (++$counter % $division == 0) {
                                $articlelistbit .= "</tr>";
                                $counter = 0;
                        } 
                } 
                if ($counter != 0) {
                        for (;$counter < $division;$counter++) {
                                $articlelistbit .= "<td></td>\n";
                        } 
                } 

                $pagelinks = makepagelink2("./search.php?action=$action", $pagenum, $totalpages);
                eval("\$pagenav = \"" . gettemplate('pagenav') . "\";");

                eval("\$articlelist = \"" . gettemplate('searchresult_articlelist') . "\";");
        } else {
                eval("\$articlelist = \"" . gettemplate('searchresult_articlelist_none') . "\";");
        } 
        eval("dooutput(\"" . gettemplate('searchresult') . "\");");
} 

if (empty($_GET[action]) AND empty($_POST[action])) {
        $templatelist = "searchhome,navbar_advancesearch";
        require "global.php";

        cachesorts();
        $sortlistoptions = sortsbit();

        eval("\$navbit = \"" . gettemplate('navbar_joiner') . "\";");
        eval("\$navbit .= \"" . gettemplate('navbar_advancesearch') . "\";");
        eval("\$navbar = \"" . gettemplate('navbar') . "\";");

        eval("dooutput(\"" . gettemplate('searchhome') . "\");");
} 

if ($_GET[action] == "result") {
        $templatelist = "searchresult,navbar_searchresult,articlelistbit,articlelistbit_img,pagenav,searchresult_articlelist";
        require "global.php"; 
        // print_rr($_GET);
        // exit;
        $keyword = trim($_GET[keyword]);
        $author = trim($_GET[author]);

        if (!pa_isset($keyword) AND !pa_isset($author)) {
                show_errormessage("error_keyword_blank");
        } 

        if (pa_isset($keyword)) {
                if (strlen($keyword) < 4) {
                        show_errormessage("error_keyword_tooshort");
                } else if (strlen($keyword) > 20) {
                        show_errormessage("error_keyword_toolong");
                } 
                $condition[] = " article.title LIKE '%" . addslashes(htmlspecialchars($keyword)) . "%'";
                $condition[] = " articletext.subhead LIKE '%" . addslashes(htmlspecialchars($keyword)) . "%' ";
                if ($_GET[type] != "title") {
                        $condition[] = " articletext.articletext LIKE '%" . addslashes(htmlspecialchars($keyword)) . "%'";
                } 
        } 

        unset($conditions);
        if (is_array($condition)) {
                $conditions = "(" . implode(" OR ", $condition) . ")";
        } 

        if (pa_isset($author)) {
                if (strlen($author) < 4) {
                        show_errormessage("error_author_tooshort");
                } else if (strlen($author) > 20) {
                        show_errormessage("error_author_toolong");
                } 
                if (trim($conditions) != "") {
                        $conditions .= " AND ";
                } 
                $conditions .= " article.author LIKE '%" . addslashes(htmlspecialchars($author)) . "%' ";
        } 

        $datelimit = intval($_GET[datelimit]);
        if ($datelimit > 0) {
                $timelimit = mktime(0, 0, 0, date("m") - $datelimit, date("d"), date("Y"));
                $conditions .= " AND date>'" . $timelimit . "' ";
        } 

        cachesorts();

        if (!empty($_GET[sortidss])) {
                $sortidss = addslashes(trim($_GET[sortidss]));
                $conditions .= " AND article.sortid IN (0$sortidss) ";
        } else {
                $sortids = $_GET[sortids];
                if (is_array($sortids)) {
                        $sortids = array_flip($sortids);

                        unset($sortidss);
                        if (!isset($sortids["-1"])) {
                                if ($_GET[subsort]) {
                                        if (is_array($sortids)) {
                                                foreach ($sortids AS $sortid => $value) {
                                                        $sortidss .= "," . getsubsorts($sortid);
                                                } 
                                        } 
                                } else {
                                        if (is_array($sortids)) {
                                                foreach ($sortids AS $sortid => $value) {
                                                        $sortidss .= ",$sortid";
                                                } 
                                        } 
                                        // print_rr($sortids);
                                } 
                                $sortidss = substr($sortidss, 1, strlen($sortidss));
                                $sortids_array = explode(",", $sortidss);
                                $sortids_array = array_flip($sortids_array);
                                $sortidss = implode(",", array_flip($sortids_array));
                                $conditions .= " AND article.sortid IN (0$sortidss) "; 
                                // echo "<br>asdf<br>";
                        } 
                } 
        } 

        /*
    echo $sortidss;
    print_rr($condition);
    echo $conditions;
    */
        $sortlist = makesortlist();

        eval("\$navbit = \"" . gettemplate('navbar_joiner') . "\";");
        eval("\$navbit .= \"" . gettemplate('navbar_searchresult') . "\";");
        eval("\$navbar = \"" . gettemplate('navbar') . "\";");

        $sql = "SELECT * FROM " . $db_prefix . "article AS article
                     LEFT JOIN " . $db_prefix . "articletext AS articletext
                     USING (articleid)
                     WHERE $conditions AND visible=1
                     GROUP BY article.articleid"; 
        // echo "<pre>$sql</pre>";
        $total = $DB->query($sql);

        $totalresults = $DB->num_rows($total);

        unset($articlelist);

        $perpage = $searchperpage;
        $totalpages = ceil($totalresults / $perpage);

        if ($pagenum < 1 OR empty($pagenum)) {
                $pagenum = 1;
        } elseif ($pagenum > $totalpages) {
                $pagenum = $totalpages;
        } 

        $offset = ($pagenum-1) * $perpage;

        if ($totalresults > 0) {
                $from = $offset + 1;
                if ($pagenum == $totalpages) {
                        $to = $totalresults;
                } else {
                        $to = $offset + $perpage;
                } 
        } else {
                $from = 0;
                $to = 0;
        } 

        $ordertype = $_GET[ordertype];
        if ($ordertype == "date") {
                $orderby = " article.date ";
        } else if ($ordertype == "title") {
                $orderby = " article.title ";
        } else {
                $orderby = " article.author ";
        } 

        if ($_GET[displayorder] != "asc") {
                $displayorder = " DESC ";
        } else {
                $displayorder = " ASC ";
        } 
        $articles = $DB->query("$sql
                                   ORDER BY binary $orderby $displayorder
                                   LIMIT $offset,$perpage");

        if ($DB->num_rows($articles) > 0) {
                $counter = 0;
                $row = 0;
                $division = 1;
                $tablewidth = floor(100 / $division);

                while ($article = $DB->fetch_array($articles)) {
                        $article[date] = padate($dateformat_article, $article[date]);
                        $article[time] = padate($timeformat_article, $article[date]);
                        if (!empty($article[imageid])) {
                                eval("\$article[img] = \"" . gettemplate('articlelistbit_img') . "\";");
                        } else {
                                $article[img] = "";
                        } 
                        if ($counter == 0) {
                                if ($row++ % 2 == 0) {
                                        $bgcolor = "$style[firstalt]";
                                } else {
                                        $bgcolor = "$style[secondalt]";
                                } 
                                $articlelistbit .= "<tr bgcolor=\"$bgcolor\" align=\"center\">";
                        } 
                        $articlelistbit .= "<td nowrap valign=\"top\" width=\"$tablewidth%\">";
                        eval("\$articlelistbit .= \"" . gettemplate('articlelistbit') . "\";");

                        $articlelistbit .= "</td>\n";

                        if (++$counter % $division == 0) {
                                $articlelistbit .= "</tr>";
                                $counter = 0;
                        } 
                } 
                if ($counter != 0) {
                        for (;$counter < $division;$counter++) {
                                $articlelistbit .= "<td></td>\n";
                        } 
                } 

                foreach ($_GET AS $k => $v) {
                        if ($k != "action" AND $k != "sortids" AND $k != "subsort") {
                                $link .= "&$k=$v";
                        } 
                } 
                $link .= "&sortidss=$sortidss"; 
                // echo $link;
                $pagelinks = makepagelink2("./search.php?action=result$link", $pagenum, $totalpages);
                eval("\$pagenav = \"" . gettemplate('pagenav') . "\";");

                eval("\$articlelist = \"" . gettemplate('searchresult_articlelist') . "\";");
        } else {
                eval("\$articlelist = \"" . gettemplate('searchresult_articlelist_none') . "\";");
                $sortlistoptions = sortsbit();
                eval("\$articlelist .= \"" . gettemplate('searchresult_form') . "\";");
        } 
        eval("dooutput(\"" . gettemplate('searchresult') . "\");");
} 

?>